//
// Created by ChenJi on 2021/1/6.
//

#include <lcd_extern.h>
#include "rtc_extern.h"
#include "tim_extern.h"
#include "usart_extern.h"
#include "TastekE16.h"
extern RTC_HandleTypeDef hrtc;
uint8_t isWaitingPerSecondTask = 0;
RTC_TimeTypeDef rtc_time_buf = {0};
RTC_DateTypeDef rtc_date_buf = {0};
TIME_STAMP CURRENT_TIME;
uint8_t time_sync_plan = 0;
uint16_t time_sync_plan_time_limit = 180;
uint16_t time_sync_plan_time_out = 0;
void setRTCIRQ() {
    isWaitingPerSecondTask = 1;
}
void kernal_time_sync_plan_per_1s(){
    if(time_sync_plan){
        time_sync_plan_time_out++;
        if(time_sync_plan_time_out >= time_sync_plan_time_limit){
            time_sync_plan_time_out = 0;
            reuqest_time();
        }
    }
}
void startSyncTimePlan(){
    time_sync_plan = 1;
    time_sync_plan_time_out = 179;
}
void stopSyncTimePlan(){
    time_sync_plan = 0;
}
void cleanRTCIRQ() {
    if (isWaitingPerSecondTask) {
        isWaitingPerSecondTask = 0;
        rtctick();
    }
}

void getTime(RTC_TimeTypeDef *def) {
    HAL_RTC_GetTime(&hrtc, def, RTC_FORMAT_BIN);
}

void getDate(RTC_DateTypeDef *def) {
    HAL_RTC_GetDate(&hrtc, def, RTC_FORMAT_BIN);
}

void syncTime(uint8_t year, uint8_t month, uint8_t date, uint8_t hour, uint8_t minute, uint8_t second) {
    update_buff_time();
    rtc_date_buf.Year = year;
    rtc_date_buf.Month = month;
    rtc_date_buf.Date = date;
    rtc_time_buf.Hours = hour;
    rtc_time_buf.Minutes = minute;
    rtc_time_buf.Seconds = second;
    HAL_RTC_SetDate(&hrtc, &rtc_date_buf, RTC_FORMAT_BIN);
    HAL_RTC_SetTime(&hrtc, &rtc_time_buf, RTC_FORMAT_BIN);
}


void updateLCDTime() {
    update_buff_time();
    LCD_UpdateTime(rtc_date_buf.Year, rtc_date_buf.Month, rtc_date_buf.Date, rtc_time_buf.Hours, rtc_time_buf.Minutes);
}

void setYear(uint8_t dat) {
    update_buff_time();
    rtc_date_buf.Year = dat;
    HAL_RTC_SetDate(&hrtc, &rtc_date_buf, RTC_FORMAT_BCD);
}

void setMonth(uint8_t dat) {
    update_buff_time();
    rtc_date_buf.Month = dat;
    HAL_RTC_SetDate(&hrtc, &rtc_date_buf, RTC_FORMAT_BCD);
}

void setDay(uint8_t dat) {
    update_buff_time();
    rtc_date_buf.Date = dat;
    HAL_RTC_SetDate(&hrtc, &rtc_date_buf, RTC_FORMAT_BCD);
}

void setMinute(uint8_t dat) {
    update_buff_time();
    rtc_time_buf.Minutes = dat;
    HAL_RTC_SetTime(&hrtc, &rtc_time_buf, RTC_FORMAT_BCD);
}

void setSecond(uint8_t dat) {
    update_buff_time();
    rtc_time_buf.Seconds = dat;
    HAL_RTC_SetTime(&hrtc, &rtc_time_buf, RTC_FORMAT_BCD);
}

void setHour(uint8_t dat) {
    update_buff_time();
    rtc_time_buf.Hours = dat;
    HAL_RTC_SetTime(&hrtc, &rtc_time_buf, RTC_FORMAT_BCD);
}

void update_buff_time() {
    HAL_RTC_GetTime(&hrtc, &rtc_time_buf, RTC_FORMAT_BIN);
    HAL_RTC_GetDate(&hrtc, &rtc_date_buf, RTC_FORMAT_BIN);
}

TIME_STAMP *getCurrentTimeSTAMP() {
    update_buff_time();
    CURRENT_TIME.day = rtc_date_buf.Date;
    CURRENT_TIME.year = rtc_date_buf.Year;
    CURRENT_TIME.month = rtc_date_buf.Month;

    CURRENT_TIME.minute = rtc_time_buf.Minutes;
    CURRENT_TIME.second = rtc_time_buf.Seconds;
    CURRENT_TIME.hour = rtc_time_buf.Hours;
    return &CURRENT_TIME;
}